<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * Class CreateOrdersTable.
 */
class CreateOrdersTable extends Migration
{
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('orders', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->comment('用户id');
            $table->integer('shop_id')->comment('商铺id');
            $table->string('order_code',64)->comment('订单编号');

            $table->decimal('initial_product_money',10,2)->default('0.00')->comment('原始产品金额');
            $table->decimal('product_money',10,2)->default('0.00')->comment('实际产品金额（已除去优惠卷金额）');
            $table->decimal('initial_freight',10,2)->nullable()->comment('原始运费金额');
            $table->decimal('freight',10,2)->nullable()->comment('实际运费金额');
            $table->decimal('order_money',10,2)->default('0.00')->comment('实际订单金额=实际产品金额+实际运费金额');
            $table->decimal('discount_money',10,2)->nullable()->comment('优惠金额=实际产品金额+实际运费金额');
            $table->integer('coupon_id')->nullable()->comment('优惠卷id');
            $table->decimal('discount_coupons_money',10,2)->nullable()->comment('优惠卷金额，优惠卷优惠体现在产品金额上');

            $table->tinyInteger('order_status')->default(1)->comment('交易状态; 1:等待付款，2: 付款完成 3: 已发货 4:确认完成 5:已取消 8:评价完成');
            $table->tinyInteger('order_type')->default(1)->comment('交易类型 1、商品订单 2、库品订单 4、配单 5、限时折扣订单');
            $table->tinyInteger('refund_status')->default(0)->comment('未发货-退款状态,0:无退款，1:申请退款，2:同意退款，3:不同意退款，4:退款完成');
            $table->tinyInteger('sales_return_status')->default(0)->comment('退货状态,0:无退货，1:申请退货，2:同意退货，3:不同意退货，4:退货完成');

            $table->integer('pay_status')->default(0)->comment('支付状态 0:未支付 1:已付款');
            $table->string('pay_method',64)->nullable()->comment('支付方法');
            $table->decimal('pay_method_fee',10,2)->nullable()->comment('支付手续费');

            $table->tinyInteger('is_claim_invoice')->default(0)->comment('是否索取发票 0不索取  1索取');

            $table->string('courier_number',32)->nullable()->comment('运单号');
            $table->string('address',512)->comment('寄送地址');
            $table->string('receiver',64)->comment('寄送联系人');
            $table->string('phone',32)->comment('寄送联系人手机号');

            $table->timestamp('order_at')->nullable()->comment('下单时间');
            $table->timestamp('pay_at')->nullable()->comment('支付时间');
            $table->timestamp('shipments_at')->nullable()->comment('发货时间');
            $table->timestamp('confirm_at')->nullable()->comment('确认完成时间');
            $table->text('remark')->nullable()->comment('备注');
            $table->tinyInteger('leading')->default(0)->comment('导入标识，0正常，1为导入');
            $table->tinyInteger('order_form_type')->default('1')->comment('订单类型：1普通订单；2刷单订单；3测试订单');
            $table->string('ip', 64)->nullable()->comment('ip');
            $table->integer('belongs')->nullable()->comment('订单归属人');
            $table->json('offer_elements')->nullable()->comment('报价要素');
            $table->tinyInteger('sys_type')->default(0)->comment('0为无系统操作，1为系统自动取消，2为系统自动收货');
            $table->decimal('cp_money',10,2)->default(0.00)->comment('用于城市合伙人计算佣金的价格');

            $table->timestamps();
            $table->softDeletes();
            $table->unique('order_code');
            $table->index('shop_id');
            $table->index('order_status');
            $table->index('created_at');
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('orders');
	}
}
